/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.model.retail;

import com.xtd.model.employment.DepartmentPost;
import com.xtd.model.employment.Employee;
import com.xtd.model.inventory.Commodity;
import com.xtd.model.inventory.CommodityHasCommodity;
import com.xtd.model.inventory.CommodityRoyalty;
import com.xtd.model.inventory.InventoryRecord;
import com.xtd.util.CommodityPropertyEnum;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.IndexColumn;

/**
 *
 * @author Lu Yuanbin
 */
@Entity
@Table(name = "order_commodity")
public class OrderCommodity implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;
    @Column(name = "number")
    private String number;
    @Column(name = "name")
    private String name;
    @Column(name = "unitPrice")
    private BigDecimal unitPrice;
    @Column(name = "quantity")
    private int quantity;
    @Column(name = "type")
    private String type;
    @Column(name = "additional")
    private boolean additional;
    @Column(name = "completed")
    private boolean completed;
    @Column(name = "retrieved")
    private boolean retrieved;
    @Column(name = "cancelled")
    private boolean cancelled;
    @Column(name = "royalty_percentage")
    private BigDecimal royaltyPercentage;
    @Column(name = "createdTime")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdTime;
    @Column(name = "property")
    @Enumerated(EnumType.STRING)
    private CommodityPropertyEnum property;
    @Column(name = "stockout")
    private int stockout;
    @Column(name = "returned_quantity")
    private int returnedQuantity;
    @Column(name = "returned_price")
    private BigDecimal returnedPrice;
    @JoinColumn(name = "order_id", referencedColumnName = "id", insertable = true, updatable = true)
    @ManyToOne
    private ShootingOrder shootingOrder;
    @JoinColumn(name = "commodity_id", referencedColumnName = "id")
    @ManyToOne
    private Commodity commodity;
    @JoinColumn(name = "createdBy", referencedColumnName = "id")
    @ManyToOne
    private Employee createdBy;
    @JoinColumn(name = "departpost", referencedColumnName = "id")
    @ManyToOne
    private DepartmentPost departpost;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.DELETE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    @IndexColumn(name = "order_server_position", base = 0)
    @JoinColumn(name = "order_commodity")
    private List<OrderServer> orderServerCollection;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.DELETE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    @IndexColumn(name = "order_commodity_position", base = 0)
    @JoinColumn(name = "super_order_commodity")
    private List<OrderCommodity> subOrderCommodityCollection;
    @JoinColumn(name = "super_order_commodity", referencedColumnName = "id", insertable = true, updatable = true)
    @ManyToOne
    private OrderCommodity superOrderCommodity;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "orderCommodity", fetch = FetchType.EAGER)
    private Set<InventoryRecord> inventoryRecordCollection;

    public OrderCommodity() {
    }

    public OrderCommodity(ShootingOrder shootingOrder, Commodity commodity, Employee createdBy) {
        this.name = commodity.getName();
        this.number = commodity.getNumber();
        this.unitPrice = commodity.getUitPrice();
        this.quantity = 1;
        this.commodity = commodity;
        this.property = commodity.getProperty();
        this.type = commodity.getType();
        this.shootingOrder = shootingOrder;
        this.createdBy = createdBy;
        this.createdTime = new Date();
        if (commodity.getCommodityRoyaltyCollection() != null && !commodity.getCommodityRoyaltyCollection().isEmpty()) {
            for (CommodityRoyalty commodityRoyalty : commodity.getCommodityRoyaltyCollection()) {
                getOrderServerCollection().add(new OrderServer(this, commodityRoyalty));
            }
        }
        if (commodity.getSubCommodityCollection() != null && !commodity.getSubCommodityCollection().isEmpty()) {
            for (CommodityHasCommodity c : commodity.getSubCommodityCollection()) {
                getSubOrderCommodityCollection().add(new OrderCommodity(this, c.getAmount(), c.getSubcommodity(), createdBy));
            }
        }
    }

    public OrderCommodity(OrderCommodity superCommodity, int quantity, Commodity commodity, Employee createdBy) {
        this.name = commodity.getName();
        this.number = commodity.getNumber();
        this.unitPrice = BigDecimal.ZERO;
        this.quantity = quantity;
        this.commodity = commodity;
        this.superOrderCommodity = superCommodity;
        this.property = commodity.getProperty();
        this.type = commodity.getType();
        this.createdBy = createdBy;
        this.createdTime = new Date();
        if (commodity.getCommodityRoyaltyCollection() != null && !commodity.getCommodityRoyaltyCollection().isEmpty()) {
            for (CommodityRoyalty commodityRoyalty : commodity.getCommodityRoyaltyCollection()) {
                getOrderServerCollection().add(new OrderServer(this, commodityRoyalty));
            }
        }
    }

    public OrderCommodity(Integer id) {
        this.id = id;


    }

    public Integer getId() {
        return id;


    }

    public void setId(Integer id) {
        this.id = id;


    }

    public String getNumber() {
        return number;


    }

    public void setNumber(String number) {
        this.number = number;


    }

    public String getName() {
        return name;


    }

    public void setName(String name) {
        this.name = name;


    }

    public BigDecimal getUnitPrice() {
        return unitPrice;


    }

    public void setUnitPrice(BigDecimal unitPrice) {
        this.unitPrice = unitPrice;


    }

    public int getQuantity() {
        return quantity;


    }

    public void setQuantity(int quantity) {

        this.quantity = quantity;


    }

    public boolean getAdditional() {
        return additional;


    }

    public void setAdditional(boolean additional) {
        this.additional = additional;


    }

    public boolean getCompleted() {

        return completed;
    }

    public String getCompletedAsString() {
        if (property.equals(CommodityPropertyEnum.加工商品)) {
            return getCompleted() ? "是" : "否";
        } else {
            return "-";
        }
    }

    public void setCompleted(boolean completed) {
        this.completed = completed;


    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);


        return hash;


    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof OrderCommodity)) {
            return false;


        }
        OrderCommodity other = (OrderCommodity) object;


        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)) || (this.id == null && other == null && this != other)) {
            return false;


        }
        return true;


    }

    @Override
    public String toString() {
        return id + " " + number + " " + name + " " + quantity + " " + unitPrice;


    }

    /**
     * @return the commodity
     */
    public Commodity getCommodity() {
        return commodity;


    }

    /**
     * @param commodity the commodity to set
     */
    public void setCommodity(Commodity commodity) {
        this.commodity = commodity;


    }

    /**
     * @return the shootingOrder
     */
    public ShootingOrder getShootingOrder() {
        return shootingOrder;


    }

    /**
     * @param shootingOrder the shootingOrder to set
     */
    public void setShootingOrder(ShootingOrder shootingOrder) {
        this.shootingOrder = shootingOrder;


    }

    /**
     * @return the packaged
     */
    public boolean isPackaged() {

        return property.equals(CommodityPropertyEnum.套系商品);
    }

    /**
     * @return the cancelled
     */
    public boolean isCancelled() {
        return cancelled;


    }

    /**
     * @param cancelled the cancelled to set
     */
    public void setCancelled(boolean cancelled) {
        this.cancelled = cancelled;


    }

    /**
     * @return the royaltyPercentage
     */
    public BigDecimal getRoyaltyPercentage() {
        return royaltyPercentage;


    }

    /**
     * @param royaltyPercentage the royaltyPercentage to set
     */
    public void setRoyaltyPercentage(BigDecimal royaltyPercentage) {
        this.royaltyPercentage = royaltyPercentage;


    }

    /**
     * @return the createdTime
     */
    public Date getCreatedTime() {
        return createdTime;


    }

    /**
     * @param createdTime the createdTime to set
     */
    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;


    }

    /**
     * @return the createdBy
     */
    public Employee getCreatedBy() {
        return createdBy;


    }

    /**
     * @param createdBy the createdBy to set
     */
    public void setCreatedBy(Employee createdBy) {
        this.createdBy = createdBy;


    }

    /**
     * @return the departpost
     */
    public DepartmentPost getDepartpost() {
        return departpost;


    }

    /**
     * @param departpost the departpost to set
     */
    public void setDepartpost(DepartmentPost departpost) {
        this.departpost = departpost;


    }

    /**
     * @return the orderServerCollection
     */
    public List<OrderServer> getOrderServerCollection() {
        if (orderServerCollection == null) {
            orderServerCollection = new ArrayList();

        }
        return orderServerCollection;


    }

    /**
     * @param orderServerCollection the orderServerCollection to set
     */
    public void setOrderServerCollection(List<OrderServer> orderServerCollection) {
        this.orderServerCollection = orderServerCollection;


    }

    /**
     * @return the subOrderCommodityCollection
     */
    public List<OrderCommodity> getSubOrderCommodityCollection() {
        if (subOrderCommodityCollection == null) {
            subOrderCommodityCollection = new ArrayList();
        }
        return subOrderCommodityCollection;


    }

    /**
     * @param subOrderCommodityCollection the subOrderCommodityCollection to set
     */
    public void setSubOrderCommodityCollection(List<OrderCommodity> subOrderCommodityCollection) {
        this.subOrderCommodityCollection = subOrderCommodityCollection;


    }

    /**
     * @return the superOrderCommodity
     */
    public OrderCommodity getSuperOrderCommodity() {

        return superOrderCommodity;


    }

    /**
     * @param superOrderCommodity the superOrderCommodity to set
     */
    public void setSuperOrderCommodity(OrderCommodity superOrderCommodity) {
        this.superOrderCommodity = superOrderCommodity;

    }

    public boolean isStock() {
        return property.equals(CommodityPropertyEnum.材料商品) || property.equals(CommodityPropertyEnum.零售商品);
    }

    /**
     * @return the property
     */
    public CommodityPropertyEnum getProperty() {
        return property;
    }

    /**
     * @param property the property to set
     */
    public void setProperty(CommodityPropertyEnum property) {
        this.property = property;
    }

    /**
     * @return the stockout
     */
    public int getStockout() {
        return stockout;
    }

    public String getStockoutAsString() {
        if (isStock()) {
            return String.valueOf(stockout);
        } else {
            return "-";
        }
    }

    /**
     * @param type the type to set
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    public String getServerInfo() {
        StringBuilder sb = new StringBuilder();
        for (OrderServer os : getOrderServerCollection()) {
            if (os.getServer() != null) {
                sb.append(os.getServer().getName()).append(";");
            }
        }
        return sb.toString();
    }

    /**
     * @return the inventoryRecordCollection
     */
    public Set<InventoryRecord> getInventoryRecordCollection() {
        return inventoryRecordCollection;
    }

    /**
     * @param inventoryRecordCollection the inventoryRecordCollection to set
     */
    public void setInventoryRecordCollection(Set<InventoryRecord> inventoryRecordCollection) {
        this.inventoryRecordCollection = inventoryRecordCollection;
    }

    /**
     * @param stockout the stockout to set
     */
    public void setStockout(int stockout) {
        this.stockout = stockout;
    }

    /**
     * @return the returnedQuantity
     */
    public int getReturnedQuantity() {
        return returnedQuantity;
    }

    /**
     * @param returnedQuantity the returnedQuantity to set
     */
    public void setReturnedQuantity(int returnedQuantity) {
        this.returnedQuantity = returnedQuantity;
    }

    /**
     * @return the returnedPrice
     */
    public BigDecimal getReturnedPrice() {
        return returnedPrice;
    }

    /**
     * @param returnedPrice the returnedPrice to set
     */
    public void setReturnedPrice(BigDecimal returnedPrice) {
        this.returnedPrice = returnedPrice;
    }

    /**
     * @return the retrieved
     */
    public boolean isRetrieved() {

        return retrieved;
    }

    public String getRetrievedAsString() {
        if (isRetrievable()) {
            return isRetrieved() ? "是" : "否";
        } else {
            return "-";
        }
    }

    public boolean isRetrievable() {
        if (property.equals(CommodityPropertyEnum.加工商品) || property.equals(CommodityPropertyEnum.零售商品) || property.equals(CommodityPropertyEnum.材料商品)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param retrieved the retrieved to set
     */
    public void setRetrieved(boolean retrieved) {
        this.retrieved = retrieved;
    }

    public boolean hasStockOutRecord() {

        if (inventoryRecordCollection != null && inventoryRecordCollection.size() > 0) {
            return true;
        }
        for (OrderCommodity oc : subOrderCommodityCollection) {
            return oc.hasStockOutRecord();
        }
        return false;
    }

    public boolean hasStockOutItem() {
        if (stockout > 0) {
            return true;
        }
        for (OrderCommodity oc : subOrderCommodityCollection) {
            return oc.hasStockOutItem();
        }
        return false;
    }
}
